﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Security;

public partial class Register : System.Web.UI.Page
{
    private bool isValid;
    protected void Page_Load(object sender, EventArgs e)
    {
        isValid = true;
        this.LoginIdTextBox.Style.Add("color", "#ccc");
        this.MobileTextBox.Style.Add("color", "#ccc");
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        if (!isValid)
        {
            return;
        }
        string userName = this.LoginIdTextBox.Text.Trim();
        User user = new User()
        {
            UserName = userName,
            Password = this.PasswordTextBox.Text,
            PhoneNumber = this.MobileTextBox.Text.Trim(),
            IsValidate = false
        };

        user.ValidateCode = UserManager.GetInstance().GenerateRandomCode();
        UserManager.GetInstance().AddUser(user);
        FormsAuthentication.SetAuthCookie(userName, false);
        Utility.SendValidateCode(user.UserName, user.PhoneNumber, user.ValidateCode);

        Response.Redirect("./Validate.aspx?From=true");
    }

    protected void userExist_ServerValidate(Object source, ServerValidateEventArgs args)
    {
        if (UserManager.GetInstance().IsUserExist(args.Value))
            args.IsValid = false;
        else
            args.IsValid = true;
        this.isValid = this.isValid && args.IsValid;
    }

    protected void validfilename_ServerValidate(Object source, ServerValidateEventArgs args)
    {
        char[] invalidChars = Path.GetInvalidFileNameChars();
        foreach (char c in invalidChars)
        {
            if (args.Value.Contains(c))
            {
                args.IsValid = false;
                break;
            }
        }
        this.isValid = this.isValid && args.IsValid;
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (Master.IsNumber(args.Value))
            args.IsValid = true;
        else
            args.IsValid = false;
        this.isValid = this.isValid && args.IsValid;
    }

}